package com.zl.tby.screen.enums;

import com.zl.tby.dto.TbyEcologyInfoQuery;
import com.zl.tby.screen.vo.ecology.ChartsData;
import com.zl.tby.screen.vo.ecology.TbyEcologyScreenInfo;
import org.noear.wood.DbContext;
import org.noear.wood.DbTableQuery;
import org.noear.wood.utils.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author changpeng
 * @version 1.0
 * @since 2024/11/6 21:25
 */
public enum TbyEcologyColumnMaxminEnum {
    temperature("气温") {
        @Override
        public List<ChartsData> search(DbContext db, TbyEcologyXAxisEnum xAxisEnum, TbyEcologyInfoQuery query) {
            List<String> xAxisList = xAxisEnum.getXAxisList(query);
            List<ChartsData> resultList = new ArrayList<>();
            Map<String, TbyEcologyScreenInfo> map = getDataMap(db, xAxisEnum, query);
            for (String s : xAxisList) {
                ChartsData data = new ChartsData();
                // 返回x轴的label
                data.setXAxis(xAxisEnum.getXAxisValue(s));
                data.setValue(map.containsKey(s) ? map.get(s).getTemperature().setScale(1, RoundingMode.HALF_UP) : null);
//生态检测数据图表接口原来只返回平均值，现在增加最高值和最低值2025年10月15日 09:57:15bjs
                TbyEcologyScreenInfo infotemp = map.get(s);
                if(infotemp != null && infotemp.getMaxTemperature()!=null){
                    data.setValueMax(map.containsKey(s) ? map.get(s).getMaxTemperature().setScale(1, RoundingMode.HALF_UP) : BigDecimal.ZERO);
                    data.setValueMin(map.containsKey(s) ? map.get(s).getMinTemperature().setScale(1, RoundingMode.HALF_UP) : BigDecimal.ZERO);
                }
                resultList.add(data);
            }
            return resultList;
        }
    },
    humidity("湿度") {
        @Override
        public List<ChartsData> search(DbContext db, TbyEcologyXAxisEnum xAxisEnum, TbyEcologyInfoQuery query) {
            List<String> xAxisList = xAxisEnum.getXAxisList(query);
            List<ChartsData> resultList = new ArrayList<>();
            Map<String, TbyEcologyScreenInfo> map = getDataMap(db, xAxisEnum, query);
            for (String s : xAxisList) {
                ChartsData data = new ChartsData();
                // 返回x轴的label
                data.setXAxis(xAxisEnum.getXAxisValue(s));
                data.setValue(map.containsKey(s) ? map.get(s).getHumidity().setScale(1, RoundingMode.HALF_UP) : BigDecimal.ZERO);
//生态检测数据图表接口原来只返回平均值，现在增加最高值和最低值2025年10月15日 09:57:15bjs
                TbyEcologyScreenInfo infotemp = map.get(s);
                if(infotemp != null && infotemp.getMaxHumidity()!=null){
                    data.setValueMax(map.containsKey(s) ? map.get(s).getMaxHumidity().setScale(1, RoundingMode.HALF_UP) : BigDecimal.ZERO);
                    data.setValueMin(map.containsKey(s) ? map.get(s).getMinHumidity().setScale(1, RoundingMode.HALF_UP) : BigDecimal.ZERO);
                }
                resultList.add(data);
            }
            return resultList;
        }
    },
    rain("降雨量") {
        @Override
        public List<ChartsData> search(DbContext db, TbyEcologyXAxisEnum xAxisEnum, TbyEcologyInfoQuery query) {
            List<String> xAxisList = xAxisEnum.getXAxisList(query);
            List<ChartsData> resultList = new ArrayList<>();
            Map<String, TbyEcologyScreenInfo> map = getDataMap(db, xAxisEnum, query);
            for (String s : xAxisList) {
                ChartsData data = new ChartsData();
                // 返回x轴的label
                data.setXAxis(xAxisEnum.getXAxisValue(s));
                data.setValue(map.containsKey(s) ? map.get(s).getRain().setScale(1, RoundingMode.HALF_UP) : BigDecimal.ZERO);
                resultList.add(data);
            }
            return resultList;
        }
    },
    oxygen("负氧离子") {
        @Override
        public List<ChartsData> search(DbContext db, TbyEcologyXAxisEnum xAxisEnum, TbyEcologyInfoQuery query) {
            List<String> xAxisList = xAxisEnum.getXAxisList(query);
            List<ChartsData> resultList = new ArrayList<>();
            Map<String, TbyEcologyScreenInfo> map = getDataMap(db, xAxisEnum, query);
            for (String s : xAxisList) {
                ChartsData data = new ChartsData();
                // 返回x轴的label
                data.setXAxis(xAxisEnum.getXAxisValue(s));
                data.setValue(map.containsKey(s) ? map.get(s).getOxygen() : null);
//生态检测数据图表接口原来只返回平均值，现在增加最高值和最低值2025年10月15日 09:57:15bjs
                TbyEcologyScreenInfo infotemp = map.get(s);
                if(infotemp != null && infotemp.getMaxOxygen()!=null){
                    data.setValueMax(map.containsKey(s) ? map.get(s).getMaxOxygen() : null);
                    data.setValueMin(map.containsKey(s) ? map.get(s).getMinOxygen() : null);
                }
                resultList.add(data);
            }
            return resultList;
        }
    },
    pm_one("PM2.5") {
        @Override
        public List<ChartsData> search(DbContext db, TbyEcologyXAxisEnum xAxisEnum, TbyEcologyInfoQuery query) {
            List<String> xAxisList = xAxisEnum.getXAxisList(query);
            List<ChartsData> resultList = new ArrayList<>();
            Map<String, TbyEcologyScreenInfo> map = getDataMap(db, xAxisEnum, query);
            for (String s : xAxisList) {
                ChartsData data = new ChartsData();
                // 返回x轴的label
                data.setXAxis(xAxisEnum.getXAxisValue(s));
                data.setValue(map.containsKey(s) ? map.get(s).getPmOne() : BigDecimal.ZERO);
                resultList.add(data);
            }
            return resultList;
        }
    },
    pm_two("PM10") {
        @Override
        public List<ChartsData> search(DbContext db, TbyEcologyXAxisEnum xAxisEnum, TbyEcologyInfoQuery query) {
            List<String> xAxisList = xAxisEnum.getXAxisList(query);
            List<ChartsData> resultList = new ArrayList<>();
            Map<String, TbyEcologyScreenInfo> map = getDataMap(db, xAxisEnum, query);
            for (String s : xAxisList) {
                ChartsData data = new ChartsData();
                // 返回x轴的label
                data.setXAxis(xAxisEnum.getXAxisValue(s));
                data.setValue(map.containsKey(s) ? map.get(s).getPmTwo() : BigDecimal.ZERO);
                resultList.add(data);
            }
            return resultList;
        }
    },
    lux("紫外线") {
        @Override
        public List<ChartsData> search(DbContext db, TbyEcologyXAxisEnum xAxisEnum, TbyEcologyInfoQuery query) {
            List<String> xAxisList = xAxisEnum.getXAxisList(query);
            List<ChartsData> resultList = new ArrayList<>();
            Map<String, TbyEcologyScreenInfo> map = getDataMap(db, xAxisEnum, query);
            for (String s : xAxisList) {
                ChartsData data = new ChartsData();
                // 返回x轴的label
                data.setXAxis(xAxisEnum.getXAxisValue(s));
                Integer value = 0;
                if (map.containsKey(s)) {
                    TbyEcologyScreenInfo info = map.get(s);
                    if (info.getLux() != null) {
                        //value = new BigDecimal(info.getLux()).multiply(new BigDecimal(126.58)).intValue();
                        value = info.getLux();
                    }
                }
                data.setValue(value);
                resultList.add(data);
            }
            return resultList;
        }
    },
    beam("光照") {
        @Override
        public List<ChartsData> search(DbContext db, TbyEcologyXAxisEnum xAxisEnum, TbyEcologyInfoQuery query) {
            List<String> xAxisList = xAxisEnum.getXAxisList(query);
            List<ChartsData> resultList = new ArrayList<>();
            Map<String, TbyEcologyScreenInfo> map = getDataMap(db, xAxisEnum, query);
            for (String s : xAxisList) {
                ChartsData data = new ChartsData();
                // 返回x轴的label
                data.setXAxis(xAxisEnum.getXAxisValue(s));
                data.setValue(map.containsKey(s) ? map.get(s).getBeam() : BigDecimal.ZERO);
                resultList.add(data);
            }
            return resultList;
        }
    },
    pressure("气压") {
        @Override
        public List<ChartsData> search(DbContext db, TbyEcologyXAxisEnum xAxisEnum, TbyEcologyInfoQuery query) {
            List<String> xAxisList = xAxisEnum.getXAxisList(query);
            List<ChartsData> resultList = new ArrayList<>();
            Map<String, TbyEcologyScreenInfo> map = getDataMap(db, xAxisEnum, query);
            for (String s : xAxisList) {
                ChartsData data = new ChartsData();
                // 返回x轴的label
                data.setXAxis(xAxisEnum.getXAxisValue(s));
                data.setValue(map.containsKey(s) ? map.get(s).getPressure().setScale(1, RoundingMode.HALF_UP) : BigDecimal.ZERO);
                resultList.add(data);
            }
            return resultList;
        }
    },
    wind_speed("风速风向") {
        @Override
        public List<ChartsData> search(DbContext db, TbyEcologyXAxisEnum xAxisEnum, TbyEcologyInfoQuery query) {
            List<String> xAxisList = xAxisEnum.getXAxisList(query);
            List<ChartsData> resultList = new ArrayList<>();
            Map<String, TbyEcologyScreenInfo> map = new HashMap<>(0);
            Map<Long, TbyEcologyScreenInfo> idMap = new HashMap(0);
            try {
                DbTableQuery tableQuery = db.table("tby_ecology_info").whereTrue().andIf(StringUtils.isNotEmpty(query.getStartDate()), "report_date >= ?", query.getStartDate() + " 00:00:00")
                        .andIf(StringUtils.isNotEmpty(query.getEndDate()), "report_date <= ?", query.getEndDate() + " 23:59:59")
                        .andIf(query.getStationId() != null, "station_id = ?", query.getStationId());
                String groupColumn = "DATE_FORMAT(report_date, '%H')";
                if (StringUtils.isNotEmpty(xAxisEnum.getGroupByColumn())) {
                    // 是否需要分组
                    tableQuery.groupBy(xAxisEnum.getGroupByColumn());
                    groupColumn = xAxisEnum.getGroupByColumn();
                }
                String column = xAxisEnum.toString().equals(TbyEcologyXAxisEnum.HOUR.toString()) ? query.getColumn() + ", wind_direction" : "max(id) as id, avg(" + query.getColumn() + ") AS " + query.getColumn();
                List<TbyEcologyScreenInfo> dataList = tableQuery.selectList(groupColumn + " AS x, " + column + "", TbyEcologyScreenInfo.class);
                List<Long> idList = new ArrayList<>();
                for (TbyEcologyScreenInfo entity : dataList) {
                    map.put(entity.getX(), entity);
                    if (entity.getId() != null) {
                        idList.add(entity.getId());
                    }
                }
                if (idList.size() > 0) {
                    dataList = db.table("tby_ecology_info").whereIn("id", idList).selectList("id, wind_direction", TbyEcologyScreenInfo.class);
                    for (TbyEcologyScreenInfo entity : dataList) {
                        idMap.put(entity.getId(), entity);
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            for (String s : xAxisList) {
                ChartsData data = new ChartsData();
                TbyEcologyScreenInfo info = map.get(s);
                // 返回x轴的label
                data.setXAxis(xAxisEnum.getXAxisValue(s));
                data.setValue(info != null ? info.getWindSpeed().setScale(1, RoundingMode.HALF_UP) : BigDecimal.ZERO);
                data.setWindDirection(info != null && info.getWindDirection() != null ? info.getWindDirection() : 0);
                if (info != null && idMap.containsKey(info.getId())) {
                    data.setWindDirection(idMap.get(info.getId()).getWindDirection());
                }
                resultList.add(data);
            }
            return resultList;
        }
    };
    private String value;
    TbyEcologyColumnMaxminEnum(String value) {
        this.value = value;
    }

    public static TbyEcologyColumnMaxminEnum getByCode(String code) {
        TbyEcologyColumnMaxminEnum[] array = values();
        for (TbyEcologyColumnMaxminEnum tbyEcologyXAxisEnum : array) {
            if (tbyEcologyXAxisEnum.toString().equals(code)) {
                return tbyEcologyXAxisEnum;
            }
        }
        return null;
    }

    public abstract List<ChartsData> search(DbContext db, TbyEcologyXAxisEnum xAxisEnum, TbyEcologyInfoQuery query);

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public static Map<String, TbyEcologyScreenInfo> getDataMap(DbContext db, TbyEcologyXAxisEnum xAxisEnum, TbyEcologyInfoQuery query) {
        Map<String, TbyEcologyScreenInfo> map = new HashMap<>(0);
        try {
            DbTableQuery tableQuery = db.table("tby_ecology_info").whereTrue().andIf(StringUtils.isNotEmpty(query.getStartDate()), "report_date >= ?", query.getStartDate() + " 00:00:00")
                    .andIf(StringUtils.isNotEmpty(query.getEndDate()), "report_date <= ?", query.getEndDate() + " 23:59:59")
                    .andIf(query.getStationId() != null, "station_id = ?", query.getStationId());
            String groupColumn = "DATE_FORMAT(report_date, '%H')";
            if (StringUtils.isNotEmpty(xAxisEnum.getGroupByColumn())) {
                // 是否需要分组
                tableQuery.groupBy(xAxisEnum.getGroupByColumn());
                groupColumn = xAxisEnum.getGroupByColumn();
            }
            String column = xAxisEnum.toString().equals(TbyEcologyXAxisEnum.HOUR.toString()) ? query.getColumn() : "avg(" + query.getColumn() + ") AS " + query.getColumn() + ",MAX(" + query.getColumn() + ") AS max_"+query.getColumn() + ",MIN(" + query.getColumn() + ") AS min_"+query.getColumn();
            List<TbyEcologyScreenInfo> dataList = tableQuery.selectList(groupColumn + " AS x, " + column, TbyEcologyScreenInfo.class);

            for (TbyEcologyScreenInfo entity : dataList) {
                map.put(entity.getX(), entity);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return map;
    }
}
